var resErr = require('../lib/util.js').resErr;
var async = require('async');
var DB = require("../lib/db_mysql").DB;
var UserService = require("../service/user").UserService;


exports.sign = function(req, res) {
	//var user_id = req.param('user_id');
	var user_id = req.session.adminuser.user_id;
	var latitude = req.param('latitude');
	var longitude = req.param('longitude');
	var remark = req.param('remark');
	if (!user_id) {
		return res.json({
			success: false,
			msg: 'user_id不能为空！'
		});
	}
	if (!latitude) {
		return res.json({
			success: false,
			msg: '纬度不能为空！'
		});
	}
	if (!longitude) {
		return res.json({
			success: false,
			msg: '经度不能为空！'
		});
	}
	var params = [];
	params.push(user_id);
	params.push(latitude);
	params.push(longitude);
	params.push(remark);
	var db = new DB();
	var sql_text = "insert into  t_sign_log  set user_id=?,sdate=curdate(),stime=curtime(),latitude=? ,longitude=?,remark=?";
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.json({
			success: true,
			msg: '签到成功！'
		});
	});
}

exports.sign_detail = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var begin_date = req.param('begin_date');
	var end_date = req.param('end_date');
	var params = [];
	params.push(user_id);


	var sql_text = "select * from t_sign_log  where user_id= ?  ";
	if (begin_date) {
		sql_text += " and sdate >= ?";
		params.push(begin_date);
	}
	if (end_date) {
		sql_text += " and sdate <= ?";
		params.push(end_date);
	}
	sql_text += ";";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});


}

exports.sign_list = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var begin_date = req.param('begin_date');
	var end_date = req.param('end_date');
	var params = [];
	params.push(user_id);


	var sql_text = "select * from t_sign  where user_id= ?  ";
	if (begin_date) {
		sql_text += " and init_date >= ?";
		params.push(begin_date);
	}
	if (end_date) {
		sql_text += " and init_date <= ?";
		params.push(end_date);
	}
	sql_text += ";";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});
}

exports.sign_list_teacher = function(req, res) {
	var user_id = req.param('user_id');
	var user_name = req.param('user_name');
	var begin_date = req.param('begin_date');
	var end_date = req.param('end_date');
	var params = [];

	var sql_text = "select a.*,b.name from t_sign a,t_user b  where 1 = 1 and a.user_id = b.id ";
	if (user_id) {
		sql_text += "and a.user_id = ?";
		params.push(user_id);
	}
	if (user_name) {
		sql_text += "and b.name = ?";
		params.push(user_name);
	}
	if (begin_date) {
		sql_text += " and a.init_date >= ?";
		params.push(begin_date);
	}
	if (end_date) {
		sql_text += " and a.init_date <= ?";
		params.push(end_date);
	}

	var limit = parseInt(req.param("limit") || req.param("pageSize") || '20');
	var page = parseInt(req.param("page") || req.param("pageNum") || '1');

	if (req.param("sort")) {
		sql_text += " order by " + req.param("sort");
		if (req.param("dir")) {
			sql_text += " " + req.param("dir");
		}
	}
	sql_text += ";";

	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});
}

exports.get_verify = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var params = [];
	params.push(user_id);


	var sql_text = "select a.* from t_group as a,t_group_user as b where a.id=b.group_id and b.user_id=?;";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});


}


exports.leave = function(req, res) {
	//var user_id = req.param('user_id');
	var user_id = req.session.adminuser.user_id;
	var leave_type = req.param('leave_type');
	var begin_date = req.param('begin_date');
	var end_date = req.param('end_date');
	var reson = req.param('reson');
	var verify = req.param('verify');

	if (!leave_type) {
		return res.json({
			success: false,
			msg: '请假类型不能为空！'
		});
	}
	if (!begin_date) {
		return res.json({
			success: false,
			msg: '开始时间不能为空！'
		});
	}
	if (!end_date) {
		return res.json({
			success: false,
			msg: '结束时间不能为空！'
		});
	}
	if (!reson) {
		return res.json({
			success: false,
			msg: '请假原因不能为空！'
		});
	}
	var params = [];
	params.push(leave_type);
	params.push(user_id);
	params.push(begin_date);
	params.push(end_date);
	params.push(reson);
	var db = new DB();

	var teacher_id;
	console.log("user_id=" + user_id);
	async.waterfall([
		function(cb) { //获取对应的老师ID
			var tmp_params = [];
			sql_text = "select a.team_leader as teacher_id from t_group as a,t_group_user as b where a.id=b.group_id and b.user_id=?;";
			tmp_params.push(user_id);
			var Mysql = db.query(sql_text, tmp_params, function(err, datas) {
				if (err) return cb(err);
				if (datas.length > 0 && datas[0].teacher_id > 0) {
					teacher_id = datas[0].teacher_id
				} else {
					//console.log(Mysql.sql);
					return cb('找不到对应审批老师');
				}
				return cb(null);
			})
		} //这里需要加判断逻辑，如重复请假，请假时间冲突等
	], function(err) {
		if (err) return resErr(res, err);
		sql_text = "insert into t_leave set leave_type=?,user_id=?,begin_date=?,end_date=?,status=0,reson=?,verify_by=?,create_date=now();";
		if (verify)
			params.push(verify);
		else params.push(teacher_id);
		db.query(sql_text, params, function(err, datas) {
			console.log(datas);
			if (err) {
				return resErr(res, err);
			}
			return res.jsonp({
				"success": true
			})
		})
	})

}



exports.leavelist_student = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var Status = req.param('status');
	var params = [];
	params.push(user_id);


	var sql_text = "select * from t_leave where user_id=? ";
	if (Status) {
		sql_text += " and status = ?";
		params.push(Status);
	}
	sql_text += ";";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});


}



exports.leavelist_teacher = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var Status = req.param('status');
	var params = [];
	params.push(user_id);


	var sql_text = "select * from t_leave where verify_by=? ";
	if (Status) {
		sql_text += " and status = ?";
		params.push(Status);
	}
	sql_text += ";";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});


}


exports.leavelist_director = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var Status = req.param('status');
	var params = [];
	params.push(user_id);


	var sql_text = "select * from t_leave where verify_by2=? ";
	if (Status) {
		sql_text += " and status = ?";
		params.push(Status);
	}
	sql_text += ";";


	var db = new DB();
	db.query(sql_text, params, function(err, datas) {
		if (err) return resErr(res, err);
		return res.jsonp(datas);
	});


}


exports.apply_leave = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var Status = req.param('status');
	var leave_id = req.param('leave_id');
	var verify_comment = req.param('verify_comment');

	if (!Status) {
		return res.json({
			success: false,
			msg: '审批状态不能为空！'
		});
	}
	if (!leave_id) {
		return res.json({
			success: false,
			msg: '请假id不能为空！'
		});
	}
	var params = [];
	var db = new DB();

	var leavedays = 0;
	var teacher_id;
	async.waterfall([
		function(cb) { //获取请假天数和。。。
			var tmp_params = [];
			tmp_params.push(leave_id);
			tmp_params.push(user_id);
			sql_text = "select TIMESTAMPDIFF(day,begin_date,end_date) as leave_days  from  t_leave where id = ? and status=0 and verify_by=?";
			var Mysql = db.query(sql_text, tmp_params, function(err, datas) {
				if (err) return cb(err);
				if (datas.length > 0) {
					leavedays = datas[0].leave_days;
				} else {
					//console.log(Mysql.sql);
					return cb('找不到对应请假单');
				}
				return cb(null);
			})
		} //这里需要加判断逻辑，如重复请假，请假时间冲突等
	], function(err) {
		if (err) return resErr(res, err);


		if (leavedays > 15 && Status == 1) //一级审批通过&请假天数大N天，需要二级审批,暂定4主任，后面再修正） ,status=4表示二级审批
		{

			sql_text = "update t_leave set status=4 ,verify_comment = ? ,verify_date=now(),verify_by2 = 4 where id = ?"
			params.push(verify_comment);
			params.push(leave_id);
		} else {
			sql_text = "update t_leave set status= ? ,verify_comment = ? ,verify_date=now() where id =?"
			params.push(Status);
			params.push(verify_comment);
			params.push(leave_id);
		}



		db.query(sql_text, params, function(err, datas) {
			if (err) {
				return resErr(res, err);
			}
			return res.jsonp({
				"success": true
			})
		})
	})

}


exports.apply_leave_director = function(req, res) {
	var user_id = req.session.adminuser.user_id;
	var Status = req.param('status');
	var leave_id = req.param('leave_id');
	var verify_comment = req.param('verify_comment');

	if (!Status) {
		return res.json({
			success: false,
			msg: '审批状态不能为空！'
		});
	}
	if (!leave_id) {
		return res.json({
			success: false,
			msg: '请假id不能为空！'
		});
	}
	var params = [];
	var db = new DB();

	async.waterfall([
		function(cb) { //获取请假单
			var tmp_params = [];
			tmp_params.push(leave_id);
			tmp_params.push(user_id);
			sql_text = "select count(*) as count  from  t_leave where id = ? and status=4 and verify_by2=?";
			var Mysql = db.query(sql_text, tmp_params, function(err, datas) {
				if (err) return cb(err);
				if (datas.length > 0) {} else {
					//console.log(Mysql.sql);
					return cb('找不到对应请假单');
				}
				return cb(null);
			})
		} //这里需要加判断逻辑，如重复请假，请假时间冲突等
	], function(err) {
		if (err) return resErr(res, err);


		sql_text = "update t_leave set status= ? ,verify_comment2 = ? ,verify_date2=now() where id =?"
		params.push(Status);
		params.push(verify_comment);
		params.push(leave_id);



		db.query(sql_text, params, function(err, datas) {
			if (err) {
				return resErr(res, err);
			}
			return res.jsonp({
				"success": true
			})
		})
	})

}



exports.edit_user = function(req, res) {

	var id = req.param('id');
	var name = req.param('name');
	var mobile = req.param('mobile');
	var location_id = req.param('location_id');
	var Class = req.param('class');
	var status = req.param('status');
	var openid = req.param('openid');
	var pic = req.param('pic');
	var ticket = req.param('ticket');
	var remark = req.param('remark');
	var user_type = req.param('user_type');

	if (!id) {
		return res.json({
			success: false,
			msg: '用户id不能为空！'
		});
	}

	var sql_text = "update t_user set id=?";
	var params = [];
	params.push(id);
	if (name) {
		sql_text += ",name=?";
		params.push(name);
	}
	if (mobile) {
		sql_text += ",mobile=?";
		params.push(mobile);
	}
	if (location_id) {
		sql_text += ",location_id=?";
		params.push(location_id);
	}
	if (Class) {
		sql_text += ",Class=?";
		params.push(Class);
	}
	if (status) {
		sql_text += ",status=?";
		params.push(status);
	}
	if (openid) {
		sql_text += ",openid=?";
		params.push(openid);
	}
	if (pic) {
		sql_text += ",pic=?";
		params.push(pic);
	}
	if (ticket) {
		sql_text += ",ticket=?";
		params.push(ticket);
	}
	if (remark) {
		sql_text += ",remark=?";
		params.push(remark);
	}
	if (user_type) {
		sql_text += ",user_type=?";
		params.push(user_type);
	}
	sql_text += " where id=?";
	params.push(id);


	var db = new DB();

	async.waterfall([
		function(cb) { //查看手机是否已经存在
			var tmp_params1 = [];
			tmp_params1.push(mobile);
			sql_text1 = "select count(*) as count  from  t_user where id = ? ";
			var Mysql = db.query(sql_text1, tmp_params1, function(err, datas) {
				if (err) return cb(err);
				if (datas.length <= 0) {
					return cb('找不到相关用户');
				}
				return cb(null);
			})
		}
	], function(err) {
		if (err) return resErr(res, err);

		db.query(sql_text, params, function(err, datas) {
			if (err) {
				return resErr(res, err);
			}
			return res.jsonp({
				"success": true
			})
		})
	})

}

